package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CoursesActivity extends Activity {

	ArrayList<NameEntity> courses;
	ArrayList<Item> courseItems;
	MyCustomAdapter dataAdapter;
	int department_id=0,student_id=0,instructor_id=0;
	String condition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_majors);
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			department_id = extras.getInt("department_id");
			condition = "?d_id="+department_id;
		}
		else
		{
			if(sip.isStudent())
			{
				student_id = sip.getUserId();
				condition = "?student_id="+student_id;
			}
			else if(sip.isInstructor())
			{
				instructor_id = sip.getUserId();
				condition = "?i_id="+instructor_id;
			}
		}
		getcourses(department_id);
	}

	public void getcourses(int department_id) {
		String serverURL = new myURL("read_courses.php"+condition,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setCourses", true);
		p.get();
	}

	public void setCourses(String s, String error) {
		Log.d("ray", "course reply: " + s);
		NameEntity ne = null;
		courses = new ArrayList<NameEntity>();
		
		ArrayList<Course> myCs = new APIManager().getCourses(s);
		for(int i =0;i<myCs.size();i++)
		{
			Log.d("ray"," Course id" +myCs.get(i).getId() + " ,name: "+myCs.get(i).getCourse_name());
			ne = new NameEntity(myCs.get(i).getId(), myCs.get(i).getCourse_name(), "course");
			ne.setDescription(myCs.get(i).getCourse_description());
			courses.add(ne);
		}
		courseItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		if (courses.size() == 0) {
			courseItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < courses.size(); i++) {
				Item it = new Item(courses.get(i).getId(), "",
						courses.get(i).toString());
				it.setCode(courses.get(i).getDescription());
				courseItems.add(it);
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.name_list,
					courseItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						if(sip.isStudent())
						{
							i = new Intent(getBaseContext(),StudentCourseActivity.class);
						}
						else
						{
							i = new Intent(getBaseContext(),CourseActivity.class);
						}
						int course_id = dataAdapter.tmpList.get(position).getId();
						i.putExtra("course_id",course_id);
						startActivity(i);

					}
				}
			});
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.instructors , menu);
		SharedMenu.onCreateOptionsMenu(this, menu, getApplicationContext(),
				dataAdapter);
		return true;

	}
	public boolean onOptionsItemSelected(MenuItem item) {

		if (SharedMenu.onOptionsItemSelected(item, this) == false) {
			// handle local menu items here or leave blank

			switch (item.getItemId()) {
			case R.id.add:
				Intent intent = new Intent(this, CourseActivity.class);
				startActivity(intent);
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}

}
